from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View
from myapp.models import *
from mydjango import settings

#from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#方法视图
def myindex(request):

    return HttpResponse('这里是首页')

# 类视图  可读性好，易于扩展
class MyView(View):
    greet = '您好，欢迎您'

    def get(self,request):
        greet = '您好，欢迎您'
        num = 200
        li = [2, 3, 4, 5, 6]
        dic = {'name': 'haha', 'age': 20}
        # return HttpResponse(self.greet)
        #渲染模板
        return render(request,'test.html',locals())

    def post(self,reqeust):
        return HttpResponse(self.greet)




#用户管理页面
#查询用户
class  Myindex(View):
    def get(self,reqeust):
        find_user = User.objects.all()
        print(find_user)
        return render(reqeust,'index.html',locals())

    def post(self,request):
        username = request.POST.get('username')
        # print(username)
        find_user = User.objects.filter(username=username)
        if find_user:
            return render(request,'find.html',locals())
        return HttpResponse('没有此用户')


#用户添加
class UserAdd(View):
    def get(self,reqeust):
        dic = [{'key':'普通用户','value':0},{'key':'管理员','value':1},{'key':'商家','value':'2'}]
        return render(reqeust,'useradd.html',locals())

    def post(self,reqeust):
        username = reqeust.POST.get('username')
        password = reqeust.POST.get('password')
        img = reqeust.FILES['img']
        type = reqeust.POST.get('type')
        # print(username)
        # print(password)
        # print(upload)
        # print(type)
        find_user = User.objects.filter(username=username).first()
        if find_user:
            return HttpResponse('用户已存在')
        else:
            User.objects.create(username=username,password=password,img=img,type=type)
            path = '%s/%s'%(settings.UPLOAD_ROOT,img)
            with open(path,'wb') as f:
                #chunks 图片文件太大分块写入
                for i in img.chunks():
                    f.write(i)
            return redirect('http://localhost:8000/Myindex/')


#修改用户
class Modify(View):
    def get(self,reqeust):
        oid = reqeust.GET.get('id')
        # print(oid)
        dic = [{'key': '普通用户', 'value': 0}, {'key': '管理员', 'value': 1}, {'key': '商家', 'value': '2'}]
        find_user = User.objects.get(id=oid)
        return render(reqeust,'modify.html',locals())

    def post(self,reqeust):
        oid = reqeust.GET.get('id')
        username = reqeust.POST.get('username')
        password = reqeust.POST.get('password')
        img = reqeust.FILES['img']
        type = reqeust.POST.get('type')
        print(oid)
        print(username)
        print(password)
        print(img)

        try:
            find_user = User.objects.get(id=oid)
            find_user.username = username
            find_user.password = password
            find_user.img = img
            find_user.type = type
            find_user.save()
        except Exception as e:
            print(e)
            return HttpResponse('修改失败')

        path = '%s/%s' % (settings.UPLOAD_ROOT, img)
        with open(path, 'wb') as f:
            # chunks 图片文件太大分块写入
            for i in img.chunks():
                f.write(i)
        return redirect('http://localhost:8000/Myindex/')


#删除用户
class Del(View):
    def get(self,reqeust):
        id = reqeust.GET.get('id')
        print(id)
        User.objects.get(id=id).delete()
        return redirect('http://localhost:8000/Myindex/')







